﻿using Ewell.BizComponent.Service.IServer;
using Ewell.BizComponent.Service.Model;
using Ewell.BizComponent.Service.Model.Dto;
using Ewell.BizComponent.Service.Model.Dto.OutSettlement;
using Ewell.BizComponent.Service.Model.Response;
using Ewell.Interface.Common.Model.Common;
using Ewell.Interface.Common.Model.GateWay.Dto.Out;
using Ewell.Interface.Common.Model.GateWay.GateWayUriInfo;
using Ewell.Interface.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ewell.BizComponent.Service.Server
{
    /// <summary>
    /// 挂号服务
    /// </summary>
    public class OutRegisterServer: IOutRegisterServer
    {
        private OutEncManageDubboService outEncManageDubboService=new OutEncManageDubboService();
        /// <summary>
        /// 门诊挂号预登记
        /// </summary>
        OutPreRegisterResponse IOutRegisterServer.PreRegister(OutPreRegisterRequest request)
        {
            OutPreRegisterResponse result =new OutPreRegisterResponse();
            List < PreRegisterOutEncounterRequest > preRegisterOutEncounters = new List < PreRegisterOutEncounterRequest >();
            foreach (var item in request.preRegist.outEncounterList)
            {
                PreRegisterOutEncounterRequest preRegisterOutEncounter=new PreRegisterOutEncounterRequest();
                preRegisterOutEncounter.attendAtEnd = item.attendAtEnd;
                preRegisterOutEncounter.attendAtBegin = item.attendAtBegin;
                preRegisterOutEncounter.msId= item.msId;
                preRegisterOutEncounter.scheduleId= item.scheduleId;
                preRegisterOutEncounter.scheduleSlotId= item.scheduleSlotId;
                preRegisterOutEncounter.providerId= item.providerId;
                preRegisterOutEncounter.providerTypeCoding= item.providerTypeCoding;
                preRegisterOutEncounter.regTypeId= item.regTypeId;
                preRegisterOutEncounter.regCategoryId= item.regCategoryId;
                preRegisterOutEncounter.insurTypeId = request.patient.insurTypeId;
                preRegisterOutEncounter.paymentTypeCoding=item.paymentTypeCoding;
                preRegisterOutEncounter.attendAtBegin= item.attendAtBegin;
                preRegisterOutEncounter.attendAtEnd= item.attendAtEnd;
                preRegisterOutEncounters.Add(preRegisterOutEncounter);
            }
            //挂号预登记
            OutEncounterPreRegistRequest preRegistRequest = new OutEncounterPreRegistRequest()
            {
                patientId= request.patient.patientId,
                patientName= request.patient.patientName,
                bizSourceCoding= request.bizSourceCoding,
                visitTypeCoding= request.preRegist.visitTypeCoding,
                outEncounterList= preRegisterOutEncounters
            };
            var gateWayPreRegist = outEncManageDubboService.outEncounterPreRegist.Post<List<PreRegisterOutPatientResponse>>(preRegistRequest);
            if (!gateWayPreRegist.success) throw new SpeedException("诊间挂号", gateWayPreRegist.message);

            List<PreSettlementDto> preSettlements = new List<PreSettlementDto>();
            foreach (var item in gateWayPreRegist.@object)
            {
                BillItemDto billItem = new BillItemDto();
                billItem.billItemId = GuidHelper.GenerateUnique19DigitId().ToString();
                billItem.isDrug = "0";
                billItem.herbNumber = 1;
                billItem.presByDocId = LoginInfo.loginUser.id;
                billItem.presByDeptId = LoginInfo.loginUser.deptCode;
                billItem.presedAt = DateTime.Now;
                List< CommodityDto > commodityDtos = new List< CommodityDto >();
                foreach (var commodity in item.commodityList)
                {
                    CommodityDto commodityDto = new CommodityDto()
                    {
                        dictFareClassNo = commodity.dictFareClassNo,
                        commoditySpec = commodity.commoditySpec,
                        medicareCheckStatus= commodity.medicareCheckStatus,
                        amount= commodity.amount,
                        clinicOrderDetailId=commodity.clinicOrderDetailId,
                        commodityCode= commodity.commodityCode,
                        commodityId= commodity.commodityId,
                        commodityName= commodity.commodityName,
                        commodityPrice= commodity.commodityPrice,
                        commodityQuantity= commodity.commodityQuantity,
                        commodityTypeCoding=commodity.commodityTypeCoding,
                        commodityUnitName= commodity.commodityUnitName,
                        needUploadFlag= commodity.needUploadFlag,
                        payAmount= commodity.payAmount,
                        presDetailId= commodity.presDetailId,
                        retailPrice= commodity.retailPrice,
                        tcmDrugUsedWay= commodity.tcmDrugUsedWay,
                    };
                    commodityDtos.Add(commodityDto);
                }
                billItem.commodityList = commodityDtos;
                PreSettlementDto settlementDto = new PreSettlementDto()
                {
                    outEncounterId= item.outEncounterId,
                    insurTypeId= item.insurTypeId,
                    paymentTypeCoding= item.paymentTypeCoding,
                    billItemList= new List<BillItemDto>() { billItem }
                };
                preSettlements.Add(settlementDto);
            }
            result.preSettlementList = preSettlements;
            return result;
        }


        /// <summary>
        /// 取消预登记
        /// </summary>
        /// <param name="outEncounterIds"></param>
        void IOutRegisterServer.PreRegistCancel(List<string> outEncounterIds)
        {
            //取消预登记
            OutEncounterPreRegistCancelRequest preRegistCancelRequest = new OutEncounterPreRegistCancelRequest()
            {
                outEncounterIds = outEncounterIds
            };
            var gateWayPreRegistCancel = outEncManageDubboService.outEncounterPreRegistCancel.Post<object>(preRegistCancelRequest);
            if (!gateWayPreRegistCancel.success) throw new SpeedException("诊间挂号", gateWayPreRegistCancel.message);
        }
    }
}
